<?php
require '../../config.php';

$resultset = mysql_query($_REQUEST['query'],$conexion);

if(!$resultset)
{
    mysql_close();
    echo json_encode('error en el query: ' . mysql_error());
}
elseif(!mysql_num_rows($resultset))
{
    mysql_close();
    echo json_encode('Sin resultados');
}
else
{
	$row = mysql_fetch_row($resultset);
	
	echo "<ul class='list-unstyled'>";
		
	for ($i=0;$i<count($row);$i++)
	{
		echo "<li>";
		echo "<label class='description' >".mysql_field_name($resultset, $i).": </label>";
		if($i==0)echo "<input id='".mysql_field_name($resultset, $i)."' class='element text medium' maxlength='25' disabled  type='text' value='"._convert($row[$i])."'>";
		else echo "<input id='".mysql_field_name($resultset, $i)."' class='element text medium' maxlength='25'  type='text' value='"._convert($row[$i])."'>";
		echo "</li>";
	}
		
	echo "</ul>";
}
function _convert($content) {
	if(!mb_check_encoding($content, 'UTF-8')
	OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {

		$content = mb_convert_encoding($content, 'UTF-8');

		if (mb_check_encoding($content, 'UTF-8')) {
			// log('Converted to UTF-8');
		} else {
			// log('Could not converted to UTF-8');
		}
	}
	return $content;
}

?>